package com.nfcuttings.presuvinil.visualactivities;

import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.nfcuttings.presuvinil.Material;
import com.nfcuttings.presuvinil.R;
import com.nfcuttings.presuvinil.database.MaterialDataSource;

public class MaterialABMAct extends ListActivity {
	private MaterialDataSource datasource;
	private EditText editDesc,editCosto, editAncho;
	private int lastSelection = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_materialabm);

		editDesc   = (EditText) findViewById(R.id.editDesc);   
		editCosto = (EditText) findViewById(R.id.editCostoMetro); 
		editAncho = (EditText) findViewById(R.id.editAncho);

		datasource = MaterialDataSource.getDataSource(this);
		datasource.open();

		List<Material> values = datasource.getAllMaterials();
		// Use the SimpleCursorAdapter to show the
		// elements in a ListView
		ArrayAdapter<Material> adapter = new ArrayAdapter<Material>(this,
				android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
		getListView().setItemsCanFocus(false);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
	}

	// Will be called via the onClick attribute
	// of the buttons in main.xml
	public void onClick(View view) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<Material> adapter = (ArrayAdapter<Material>) getListAdapter();
		Material material = null;
		switch (view.getId()) {
		case R.id.add:
			//String[] Materials = new String[] { "Cool", "Very nice", "Hate it" };
			//int nextInt = new Random().nextInt(3);
			// Save the new Material to the database
			try {
				material = datasource.createMaterial(editDesc.getText().toString(), Double.valueOf(editCosto.getText().toString()), Double.valueOf(editAncho.getText().toString()));
				adapter.add(material);
				limpiarCampos();
				break;
			} catch (Exception e) {
				mostrarMensage("Error, falta ingresar alguno de los campos requeridos.");
				break;
			}

		case R.id.delete:
			if(lastSelection > -1){
				material = (Material)getListAdapter().getItem(lastSelection);
				datasource.deleteMaterial(material);
				adapter.remove(material);
				lastSelection = -1;
				break;
			}else{
				mostrarMensage("Error, debe seleccionar un Material a borrar");
				break;
			}
		}
		adapter.notifyDataSetChanged();
	}
	
	@Override
	  public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		lastSelection = position;
	  }

	private void limpiarCampos() {
		editDesc.setText("");   
		editCosto.setText("");
		editAncho.setText("");
	}

	@Override
	protected void onResume() {
		datasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		super.onPause();
	}

	private void mostrarMensage(String string) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);

		dialog.setMessage(string);
		dialog.setCancelable(true);

		dialog.show();
	}
}
